1. Field of the Invention
The present invention relates generally to the field of computer software, and more particularly to inheritable thread-local storage.
2. Description of the Related Art
Modern multitasking operating systems support multiple threads, which allow many activities to execute simultaneously. For example, searches on multiple remote databases may all be launched by one task, each search corresponding to a unique thread, and executed simultaneously. Oftentimes, it is convenient to have storage that is unique to each thread. Unfortunately, most programming languages do not support such storage. However, there is a standard software mechanism for threads to associate implicit scope information with themselves, known variously as xe2x80x9cthread-local dataxe2x80x9d or xe2x80x9cthread-specific data.xe2x80x9d This facility exists in many threading facilities, including POSIX Threads (xe2x80x9cPthreadsxe2x80x9d) and Win32 Threads.
Under POSIX, thread-specific data allows each thread to have a separate copy of a variable, which is indexed by a common xe2x80x9ckeyxe2x80x9d value. A key is created and each thread independently sets or gets its own unique value for that key. The key is the same for all the threads, but each thread can associate its own unique value with the shared key. Each thread can change its private value for a key without affecting the key or any other thread""s value for the key. When the threading system is asked for a new key, a new variable is created. This type of xe2x80x9ckeyxe2x80x9d system is not secure, however, since the keys can be forged and data accessed without permission. The POSIX thread-specific data mechanism is explained in further detail in Programming in POSIX Threads, David R. Butenhof, Addison-Wesley, 1997, herein incorporated by reference. A similar mechanism is available in the Windows 95 operating system, and is discussed in Programming Windows 95, Charles Petzold, Microsoft Press, 1996, herein incorporated by reference.
The Java(trademark) programming language, created by Sun Microsystems, is somewhat unique among programming languages in that it has built-in thread-local storage support. Specifically, there is a public class xe2x80x9cThreadLocalxe2x80x9d that provides thread-local variables. The thread-local variables differ from the other xe2x80x9cnormalxe2x80x9d Java variables in that each thread that accesses a thread-local variable (via a get or set method) has its own, independently initialized copy of the variable. ThreadLocal objects are typically private static variables in classes that want to associate a state with a thread, such as a user ID or a transaction ID. Each thread holds an implicit reference to its copy of a ThreadLocal as long as the thread is in existence and is accessible. After a thread terminates, all of its copies of ThreadLocal variables are subject to garbage collection, unless other references to these copies exist. FIG. 3 is a table illustrating the API specification for the class ThreadLocal.
For many programming applications, is often desirable for threads to carry with them some implicit xe2x80x9cscopexe2x80x9d information, such as the principal or transaction on whose behalf the thread is executing. When a thread (xe2x80x9cthe parentxe2x80x9d) creates another thread (xe2x80x9cthe childxe2x80x9d), it is often desirable that this scope information be automatically transmitted from parent to child, in a manner that may depend on the details of the scope information in question. The thread-specific data (hereinafter xe2x80x9cthread-local dataxe2x80x9d or xe2x80x9cthread-local storagexe2x80x9d) mechanisms described above do not solve this problem, however, since values are not passed from parent threads to child threads.
In view of the foregoing, it would be desirable to have a thread-local storage mechanism in which a child thread can inherit its parent""s values directly, or in which a child thread could inherit values which are some functions of the parent""s values.
In a threading mechanism, the present invention is a system and method for providing automatic value inheritance when a parent thread creates a child thread. Upon the creation of a child thread, the system iterates over all of the inheritable thread-local values associated with the parent thread and initializes the child""s values of these inheritable thread-local values, based on an appropriate childValue method.
In a first embodiment, for each thread, a hash table maps each thread local object to a value. In a preferred implementation, the hash table is two separate logical mapsxe2x80x94one for inheritable values and one for non-inheritable values. When a thread creates a child, the system iterates over the inheritable value map to create the child""s values. An inheritance protocol (i.e. the xe2x80x9cchildValuexe2x80x9d method) may be performed on the values in order to calculate the child""s value as a function of the parent""s value, if desired.
In an alternative embodiment, the two hash tables in the first embodiment can be combined into one table, with each entry having a flag to identify the inheritable values.
In a second embodiment of the present invention, for each thread-local variable, a hash table maps each thread to a value. An object reference to a current thread is used as a look-up key in the hash table to find the value associated with this thread. For each thread, a linked list called xe2x80x9cvaluesxe2x80x9d links all the inheritable thread-local values associated with the thread. The head pointer to the linked list of inheritable values is stored in the thread object. When a parent thread creates a child thread, the system iterates over the linked list of thread-local values pertaining to the parent thread. For each parent value, a xe2x80x9cchildValuexe2x80x9d method is invoked to initialize the associated child""s value.
In a preferred embodiment, the present invention is implemented in the Java(trademark) programming language. The InheritableThreadLocal class of the present invention extends the ThreadLocal class to provide inheritance of values from a parent thread to a child thread. When a child thread is created, the child receives initial values for all InheritableThreadLocals for which the parent has values. Normally, the child""s values will be identical to the parent""s values. However, the child""s value can be made to be an arbitrary function of the parent""s value by overriding the xe2x80x9cchildValuexe2x80x9d method in the InheritableThreadLocal class.